# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=angleproject
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
pkgver=2.1.r8455
pkgrel=1
pkgdesc='ANGLE project built from git source (mingw-w64)'
arch=('any')
url='https://chromium.googlesource.com/angle/angle'
license=('LGPLv2+')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" 'gyp-git' 'git' 'python2' 'python2-setuptools')
provides=("${MINGW_PACKAGE_PREFIX}-angleproject")
conflicts=("${MINGW_PACKAGE_PREFIX}-angleproject")
options=('strip' 'staticlibs')
source=("angleproject"::git+https://chromium.googlesource.com/angle/angle
        0000-build-fix.patch
        angleproject-include-import-library-and-use-def-file.patch
        0001-static-build-workaround.patch
        0002-redist.patch)
sha256sums=('SKIP'
            'e48b3553cb956fba0881f851efadf10f8353a876bd714dbd8f0e904e16aba1e9'
            'c4a750d0a4f15148987d765df7c5a91d33ffe196210738394c3465f666575ae5'
            '42a9e7d4e4365ce119fe5b036a088f226e35b0f6bb51913f3635e01fb53ef1a6'
            'dc9ac8ffa9df3bab614cb14d2743804e26a84d5cb1a644b66788919917b9c3cd')
pkgver() {
  cd "${srcdir}"/${_realname}
  local _major=$(head -n 14 src/common/version.h | grep 'ANGLE_MAJOR_VERSION' | sed -e 's/.* //' | tr '\n' '.' | sed 's/.$/\n/')
  local _minor=$(head -n 14 src/common/version.h | grep 'ANGLE_MINOR_VERSION' | sed -e 's/.* //' | tr '\n' '.' | sed 's/.$/\n/')
  printf "%s.%s.r%s" "$_major" "$_minor" "$(git rev-list --count HEAD)"
}

prepare() {
  cd "${srcdir}"/${_realname}

  ### Fedora team patches ###
  patch -p1 -i ${srcdir}/0000-build-fix.patch

  # Make sure an import library is created and the correct .def file is used during the build
  patch -p1 -i ${srcdir}/angleproject-include-import-library-and-use-def-file.patch

  # Static build workaround
  patch -p1 -i ${srcdir}/0001-static-build-workaround.patch
  
  #Out of source directory fix
  patch -p1 -i ${srcdir}/0002-redist.patch
  }

build() {
# Set ar/compiler and architecture specific compiler flags
  cd "${srcdir}"/${_realname}
  export PYTHON=/usr/bin/python2

  sed -i -e 's_python _python2 _g' -e 's_"python"_"python2"_g' -e "s_'python'_'python2'_g" -e 's_/usr/bin/python_/usr/bin/python2_g' $(find -not \( -path "./.git*" -prune \) -type f)

  if [[ "${MINGW_CHOST}" == "i686-w64-mingw32" ]]; then
    _target="win32"
  else
    _target="win64"
  fi
  
  mkdir -p "${srcdir}"/${_realname}/build-${MINGW_CHOST}-shared
  mkdir -p "${srcdir}"/${_realname}/build-${MINGW_CHOST}-static

#Build shared libraries
  gyp -D is_debug=false -D angle_enable_vulkan=0 -D use_ozone=0 -D OS=win -D MSVS_VERSION="" -D TARGET=${_target} --format make --generator-output="${srcdir}/${_realname}/build-${MINGW_CHOST}-shared" --depth . -I "${srcdir}/${_realname}/gyp/common.gypi" "${srcdir}/${_realname}/src/angle.gyp"
  
  gyp -D is_debug=false -D angle_enable_vulkan=0 -D use_ozone=0 -D OS=win -D MSVS_VERSION="" -D TARGET=${_target} --format make --generator-output="${srcdir}/${_realname}/build-${MINGW_CHOST}-static" --depth . -I "${srcdir}/${_realname}/gyp/common.gypi" "${srcdir}/${_realname}/src/angle.gyp" -D angle_gl_library_type=static_library

  LINK=g++ make -C "${srcdir}/${_realname}/build-${MINGW_CHOST}-shared" -j$(($(nproc)+1)) V=1 CXXFLAGS="-O2 -g -pipe -Wall -std=c++14 -msse2 -DUNICODE -D_UNICODE"
cd "${srcdir}"/${_realname}  
  LINK=g++ make -C "${srcdir}/${_realname}/build-${MINGW_CHOST}-static" -j$(($(nproc)+1)) V=1 CXXFLAGS="-O2 -g -pipe -Wall -std=c++14 -msse2 -DUNICODE -D_UNICODE"
  
}

package() {
  cd "${srcdir}"/${_realname}

  mkdir -p "${pkgdir}/${MINGW_PREFIX}"/{bin,lib,include}

  install "${srcdir}"/${_realname}/build-${MINGW_CHOST}-shared/out/Debug/lib.target/libGLESv2.dll "${pkgdir}/${MINGW_PREFIX}"/bin/libGLESv2.dll
  install "${srcdir}"/${_realname}/build-${MINGW_CHOST}-shared/out/Debug/lib.target/libEGL.dll "${pkgdir}/${MINGW_PREFIX}"/bin/libEGL.dll
  ${MINGW_PREFIX}/bin/strip --strip-unneeded "${pkgdir}/${MINGW_PREFIX}"/bin/*.dll

  install "${srcdir}"/${_realname}/build-${MINGW_CHOST}-shared/libEGL.dll.a "${pkgdir}/${MINGW_PREFIX}"/lib/
  install "${srcdir}"/${_realname}/build-${MINGW_CHOST}-shared/libGLESv2.dll.a "${pkgdir}/${MINGW_PREFIX}"/lib/
  ${MINGW_PREFIX}/bin/strip --strip-unneeded "${pkgdir}/${MINGW_PREFIX}"/lib/*.dll.a
  
  install "${srcdir}"/${_realname}/build-${MINGW_CHOST}-static/out/Debug/obj.target/src/{libGLESv2.a,libEGL.a} "${pkgdir}/${MINGW_PREFIX}"/lib/
  ${MINGW_PREFIX}/bin/strip --strip-unneeded "${pkgdir}/${MINGW_PREFIX}"/lib/{libGLESv2.a,libEGL.a}
  cp -Rv include/* "${pkgdir}/${MINGW_PREFIX}"/include/
}
